package com.pureut.storage.controller;


import com.pureut.common.core.web.controller.BaseController;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.core.web.page.TableDataInfo;
import com.pureut.common.log.annotation.Log;
import com.pureut.common.log.enums.BusinessType;
import com.pureut.common.security.annotation.RequiresPermissions;
import com.pureut.storage.dto.PickingPurchaseDto;
import com.pureut.storage.export.PickingPurchaseExport;
import com.pureut.storage.service.PickingPurchaseService;
import com.pureut.storage.util.ExcelUtil;
import com.pureut.storage.vo.OrderReviewVo;
import com.pureut.storage.vo.PickingPurchaseVo;
import com.pureut.storage.vo.PickingRestDbVo;
import com.pureut.storage.vo.UpdateDocVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/** 采购退货控制层
 * @author: CM
 * @date: 2022/12/30  11:26
 */

@RestController
@RequestMapping("/pickingPurchase")
public class PickingPurchaseController extends BaseController {

    @Resource
    private PickingPurchaseService pickingPurchaseService;

    /**
     * 新增采购退货单
     */
    @RequiresPermissions("warehouse:outbound:purreturn:add")
    @Log(title = "新增采购退货单", businessType = BusinessType.INSERT)
    @PostMapping("/addPurchase")
    public AjaxResult addPurchase(@RequestBody PickingPurchaseVo pickingPurchaseVo) throws Exception {
        return toAjax(pickingPurchaseService.addPurchase(pickingPurchaseVo));
    }

    /**
     * 新增采购退货单(fegin调用)
     */
    @Log(title = "新增采购退货单(fegin调用)", businessType = BusinessType.INSERT)
    @PostMapping("/addPurchaseFeign")
    public AjaxResult addPurchaseFeign(@RequestBody PickingPurchaseVo pickingPurchaseVo) throws Exception {
        return toAjax(pickingPurchaseService.addPurchaseFeign(pickingPurchaseVo));
    }

    /**
     * 关结采购退货单
     */
    @RequiresPermissions("warehouse:outbound:purreturn:close")
    @Log(title = "关结采购退货单", businessType = BusinessType.UPDATE)
    @PostMapping("/closePurchase")
    public AjaxResult closePurchase(@RequestBody PickingPurchaseVo pickingPurchaseVo) throws Exception {
        return toAjax(pickingPurchaseService.closePurchase(pickingPurchaseVo));
    }


    /**
     * 修改采购退货单
     */
    @RequiresPermissions("warehouse:outbound:purreturn:edit")
    @Log(title = "修改采购退货单", businessType = BusinessType.UPDATE)
    @PostMapping("/updatePurchase")
    public AjaxResult updatePurchase(@RequestBody PickingPurchaseVo pickingPurchaseVo) throws Exception {
        return toAjax(pickingPurchaseService.updatePurchase(pickingPurchaseVo));
    }

    /**
     * 删除单据
     */
    @RequiresPermissions("warehouse:outbound:purreturn:remove")
    @Log(title = "删除采购退货单", businessType = BusinessType.DELETE)
    @DeleteMapping("/deletePurchase/{ids}")
    public AjaxResult deletePurchase(@PathVariable("ids") String ids) throws Exception {
        return toAjax(pickingPurchaseService.deletePurchase(ids));
    }

    /**
     * 查看单据
     */
    @RequiresPermissions("warehouse:outbound:purreturn:query")
    @GetMapping("/view/{id}")
    public AjaxResult view(@PathVariable("id") Long id) throws Exception {
        return AjaxResult.success(pickingPurchaseService.view(id));
    }

    /**
     * 查看单据列表
     */
    @GetMapping("/list")
    public TableDataInfo getList(PickingPurchaseVo pickingPurchaseVo) {
        startPage();
        //查询采购退货列表
        List<PickingPurchaseDto> list = pickingPurchaseService.getList(pickingPurchaseVo);
        return getDataTable(list);
    }

    /**
     * 提交审核
     */
    @RequiresPermissions("warehouse:outbound:purreturn:submit")
    @Log(title = "提交采购退货单审核", businessType = BusinessType.UPDATE)
    @GetMapping("/submitPurchase/{ids}")
    public AjaxResult submitPurchase(@PathVariable("ids") String ids) throws Exception {
        return toAjax(pickingPurchaseService.submitPurchase(ids));
    }

    /**
     * 审核单据
     */
    @RequiresPermissions("warehouse:outbound:purreturn:examine")
    @Log(title = "审核采购退货单据", businessType = BusinessType.UPDATE)
    @PutMapping("/purchaseReview")
    public AjaxResult purchaseReview(@Validated @RequestBody OrderReviewVo orderReviewVo) {
        return toAjax(pickingPurchaseService.purchaseReview(orderReviewVo));
    }

    /**
     * 导出
     */
    @PostMapping("/export")
    @RequiresPermissions("warehouse:outbound:purreturn:export")
    public AjaxResult inspectionMessageExport(HttpServletResponse response,PickingPurchaseVo pickingPurchaseVo) throws IOException {
        List<PickingPurchaseExport> infoExport = pickingPurchaseService.getInfoExport(pickingPurchaseVo);
        ExcelUtil<PickingPurchaseExport> excelUtil = new ExcelUtil<PickingPurchaseExport>(PickingPurchaseExport.class);
        excelUtil.excelWriter(response, infoExport, "采购退货信息.xlsx");
        return AjaxResult.success();
    }

    /**
     * Pda出库
     */
    @RequiresPermissions("warehouse:outbound:purreturn:outbound")
    @Log(title = "采购退货Pda出库", businessType = BusinessType.UPDATE)
    @PostMapping("/outPurchase")
    public AjaxResult outPurchase(@RequestBody PickingRestDbVo pickingRestDbVo) throws Exception {
        return AjaxResult.success(pickingPurchaseService.outPurchase(pickingRestDbVo));
    }

    /**
     * 采购退货拣货完成
     */
    @GetMapping("/finishProduction/{id}")
    public AjaxResult finishProduction(@PathVariable("id") Long id) throws Exception {
        return AjaxResult.success(pickingPurchaseService.subcontractingPurchase(id));
    }


    /**
     * 采购退货交接
     */
    @RequiresPermissions("warehouse:outbound:purreturn:handover")
    @Log(title = "采购退货交接", businessType = BusinessType.UPDATE)
    @PostMapping("/purchaseHandover")
    public AjaxResult purchaseHandover(@RequestBody PickingRestDbVo pickingRestDbVo) throws Exception {
        return AjaxResult.success(pickingPurchaseService.purchaseHandover(pickingRestDbVo));
    }

    /**
     * 修改检验单状态Feign
     */
    @Log(title = "修改单据", businessType = BusinessType.UPDATE)
    @PostMapping("/purchaseUpdateStatus")
    public AjaxResult purchaseUpdateStatus(@RequestBody UpdateDocVo updateDocVo) throws Exception {
        return toAjax(pickingPurchaseService.purchaseUpdateStatus(updateDocVo));
    }

}
